package com.fancy.playlist.controller;


import com.fancy.common.dto.PageResult;
import com.fancy.common.dto.UserCollectionResponseDTO;
import com.fancy.common.dto.response.Result;
import com.fancy.playlist.service.UserCollectionService;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

@Api(tags = "用户收藏管理")
@RestController
@RequestMapping("/api/v1/collections")
@RequiredArgsConstructor
public class UserCollectionController {

    private final UserCollectionService userCollectionService;

    @ApiOperation(value = "获取用户收藏列表", notes = "获取用户的收藏内容，支持按类型筛选")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "userId", value = "用户ID", required = true, paramType = "query", dataType = "long"),
        @ApiImplicitParam(name = "type", value = "收藏类型(1-歌曲,2-专辑,3-歌单,4-歌手)", paramType = "query", dataType = "int"),
        @ApiImplicitParam(name = "page", value = "页码", paramType = "query", dataType = "int", example = "1"),
        @ApiImplicitParam(name = "size", value = "每页大小", paramType = "query", dataType = "int", example = "20")
    })
    @GetMapping
    public Result<PageResult<UserCollectionResponseDTO>> getUserCollections(
            @RequestParam Long userId,
            @RequestParam(defaultValue = "1",required = false) Integer type,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "20") int size) {
        PageResult<UserCollectionResponseDTO> result = userCollectionService.getUserCollections(userId, type, page, size);
        return Result.success(result);
    }

    @ApiOperation(value = "添加收藏", notes = "用户收藏歌曲、专辑、歌单或歌手")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "userId", value = "用户ID", required = true, paramType = "query", dataType = "long"),
        @ApiImplicitParam(name = "targetId", value = "目标ID", required = true, paramType = "query", dataType = "long"),
        @ApiImplicitParam(name = "targetType", value = "目标类型(1-歌曲,2-专辑,3-歌单,4-歌手)", required = true, paramType = "query", dataType = "int")
    })
    @PostMapping
    public Result<Void> addCollection(
            @RequestParam Long userId,
            @RequestParam Long targetId,
            @RequestParam Integer targetType) {
        boolean success = userCollectionService.addCollection(userId, targetId, targetType);
        if (success) {
            return Result.success(null);
        } else {
            return Result.fail("收藏失败，可能已经收藏过了");
        }
    }

    @ApiOperation(value = "取消收藏", notes = "用户取消收藏的内容")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "userId", value = "用户ID", required = true, paramType = "query", dataType = "long"),
        @ApiImplicitParam(name = "targetId", value = "目标ID", required = true, paramType = "query", dataType = "long"),
        @ApiImplicitParam(name = "targetType", value = "目标类型(1-歌曲,2-专辑,3-歌单,4-歌手)", required = true, paramType = "query", dataType = "int")
    })
    @DeleteMapping
    public Result<Void> removeCollection(
            @RequestParam Long userId,
            @RequestParam Long targetId,
            @RequestParam Integer targetType) {
        boolean success = userCollectionService.removeCollection(userId, targetId, targetType);
        if (success) {
            return Result.success(null);
        } else {
            return Result.fail("取消收藏失败");
        }
    }

    @ApiOperation(value = "检查收藏状态", notes = "检查用户是否已收藏指定内容")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "userId", value = "用户ID", required = true, paramType = "query", dataType = "long"),
        @ApiImplicitParam(name = "targetId", value = "目标ID", required = true, paramType = "query", dataType = "long"),
        @ApiImplicitParam(name = "targetType", value = "目标类型(1-歌曲,2-专辑,3-歌单,4-歌手)", required = true, paramType = "query", dataType = "int")
    })
    @GetMapping("/check")
    public Result<Boolean> checkCollection(
            @RequestParam Long userId,
            @RequestParam Long targetId,
            @RequestParam Integer targetType) {
        boolean isCollected = userCollectionService.isCollected(userId, targetId, targetType);
        return Result.success(isCollected);
    }

    @ApiOperation(value = "获取收藏统计", notes = "获取用户收藏���总数量")
    @ApiImplicitParam(name = "userId", value = "用户ID", required = true, paramType = "query", dataType = "long")
    @GetMapping("/count")
    public Result<Integer> getCollectionCount(@RequestParam Long userId) {
        int count = userCollectionService.getCollectionCount(userId);
        return Result.success(count);
    }

    /**
     * 内部接口 - 批量检查收藏状态
     */
    @ApiIgnore
    @PostMapping("/internal/batch-check")
    public Result<Boolean> batchCheckCollection(@RequestBody CollectionBatchCheckDTO dto) {
        // 这里可以扩展批量检查功能
        return Result.success(userCollectionService.isCollected(dto.getUserId(), dto.getTargetId(), dto.getTargetType()));
    }
}

// 用于批量检查的DTO
class CollectionBatchCheckDTO {
    private Long userId;
    private Long targetId;
    private Integer targetType;

    // getters and setters
    public Long getUserId() { return userId; }
    public void setUserId(Long userId) { this.userId = userId; }
    public Long getTargetId() { return targetId; }
    public void setTargetId(Long targetId) { this.targetId = targetId; }
    public Integer getTargetType() { return targetType; }
    public void setTargetType(Integer targetType) { this.targetType = targetType; }
}
